package com.taiyuan.tongcheng.ui;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpConfig;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.utils.KJLoger;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;

import com.taiyuan.tongcheng.application.TCBroadcastAction;
import com.taiyuan.tongcheng.http.HttpParamUtil;
import com.taiyuan.tongcheng.http.TcWebservice;
import com.taiyuan.tongcheng.http.WebData;
import com.taiyuan.tongcheng.jsonparser.TcJsParser;
import com.tytc56.user.R;

public class MarkActivity extends BaseActivity{
	
	//===widget
	@BindView(id=R.id.rataingBar)
	private RatingBar  ratingBar;
	@BindView(id=R.id.btn_sub,click=true)
	private Button  subBtn;
	
	//===订单编号
	/**订单编号TAG***/
	public static final String TAG_MARK_ORDER_ID="order_id";
	/**订单编号TAG***/
	private String orderNo ="";
	/**订单编号FROM TAG***/
	public static final String TAG_MARK_ORDER_FROM="order_come_from";
	
	//===跳转来源
	private int  comFlag=0;
	// comFlag style 
	public static final int ORDER_FROM_CARGO=1;
	public static final int ORDER_FROM_MOVING=2;
	
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_remark);
	}
	
	@Override
	public void initData() {
		super.initData();
		orderNo=getIntent().getStringExtra(TAG_MARK_ORDER_ID);
		comFlag=getIntent().getIntExtra(TAG_MARK_ORDER_FROM, 0);
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.btn_sub:
			if(comFlag==ORDER_FROM_CARGO){
				markCargoOrder(orderNo, "",ratingBar.getRating());
			}else if(comFlag==ORDER_FROM_MOVING){
				markMovingOrder(orderNo, "",ratingBar.getRating());
			}
			break;

		default:
			break;
		}
	}
	
	
	@Override
	public void initWidget() {
		super.initWidget();
		
		initView();
		topTv.setText("订单评价");
		rightTv.setVisibility(View.GONE);
		 ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()  
	        {  
	            //当拖动条的滑块位置发生改变时触发该方法  
	            @Override  
	            public void onRatingChanged(RatingBar arg0  
	                , float rating, boolean fromUser)  
	            {  
	              KJLoger.debug("评价"+rating);
	            }  
	        });   
	}
	
	
	
	/**
	 * 
	* @Description: 评价搬家订单
	* @param orderId
	* @return void
	* @throws
	* @autour wzq
	* @date 2015-9-21 上午9:22:26
	* @update (date)
	 */
	private void markCargoOrder(String orderId,String markNote,float score) {
		HttpConfig config = new HttpConfig();
		config.cacheTime = 0;
		KJHttp kjHttp = new KJHttp(config);
        HttpParams params =HttpParamUtil.getCarGoMarkParams(getApplicationContext(), orderId, markNote, (int)score);
        kjHttp.post(TcWebservice.WEBSERVER_IP+TcWebservice.API.FREIGHT_ORDER_MARK, params, new HttpCallBack() {
			@Override
			public void onFailure(int errorNo, String strMsg) {
				super.onFailure(errorNo, strMsg);
			}
			@Override
					public void onSuccess(String t) {
						try {
							String date = URLDecoder.decode(t, "UTF-8");
							KJLoger.debug(date);
							WebData webdata = TcJsParser.getWedataData(date);
							if (webdata.getStatus() == 1) {
								 sendBroadcast(new Intent(TCBroadcastAction.SUCCESS_CANCEL_CARGO));
								MarkActivity.this.finish();
							} else {
								showErroCustomToast(webdata.getMsg());
							}
							if (TcLoaddingDialog.isShowing()) {
								TcLoaddingDialog.cancel();
							}
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
						super.onSuccess(t);
					}
        
		});
	}
	
	
	/**
	 * 
	* @Description: 评价搬家订单
	* @param orderId
	* @return void
	* @throws
	* @autour wzq
	* @date 2015-9-21 上午9:22:26
	* @update (date)
	 */
	private void markMovingOrder(String orderId,String markNote,float score) {
		HttpConfig config = new HttpConfig();
		config.cacheTime = 0;
		KJHttp kjHttp = new KJHttp(config);
        HttpParams params =HttpParamUtil.getMovingMarkParams(getApplicationContext(), orderId, markNote,(int) score);
        kjHttp.post(TcWebservice.WEBSERVER_IP+TcWebservice.API.MOVING_ORDER_MARK, params, new HttpCallBack() {
			@Override
			public void onFailure(int errorNo, String strMsg) {
				super.onFailure(errorNo, strMsg);
			}
			@Override
					public void onSuccess(String t) {
						try {
							String date = URLDecoder.decode(t, "UTF-8");
							KJLoger.debug(date);
							WebData webdata = TcJsParser.getWedataData(date);
							if (webdata.getStatus() == 1) {
								 sendBroadcast(new Intent(TCBroadcastAction.SUCCESS_CANCEL_CARGO));
								MarkActivity.this.finish();
							} else {
								showErroCustomToast(webdata.getMsg());
							}
							if (TcLoaddingDialog.isShowing()) {
								TcLoaddingDialog.cancel();
							}
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
						super.onSuccess(t);
					}
		});
	}
	

}
